﻿using System;
using System.Collections.Generic;
using System.Linq;

using Kingdee.BOS.Util;

namespace Hamster.K3Cloud.WebAPI.ServerExtend
{
    public class Checker
    {
        /// <summary>
        /// 检查客户端传递过来的参数。
        /// 客户端参数为JSON格式
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static Result clientParameterChecker(string parameters)
        {
            Result result = new Result();
            if (parameters == null)
            {
                result.msg = "接口请求参数不能为空";
                return result;
            }

            if (parameters.IsEmpty())
            {
                result.msg = "接口请求参数不能为空";
                return result;
            }

            if (!JSONTool.IsJson(parameters))
            {
                result.msg = "接口请求参数JSON格式有误";
                return result;
            }

            result.success = true;
            return result;
        }

        /// <summary>
        /// 客户端参数处理
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Parameter parameterHandler(Parameter parameter)
        {
            if (parameter.LocalFormId == null)
            {
                parameter.LocalFormId = "";
            } else
            {
                parameter.LocalFormId = parameter.LocalFormId.Trim();
            }

            if (parameter.NextFormId == null)
            {
                parameter.NextFormId = "";
            } else
            {
                parameter.NextFormId = parameter.NextFormId.Trim();
            }

            if (parameter.RuleId == null)
            {
                parameter.RuleId = "";
            } else
            {
                parameter.RuleId = parameter.RuleId.Trim();
            }

            if (parameter.NextEntryKey == null)
            {
                parameter.NextEntryKey = "";
            } else
            {
                parameter.NextEntryKey = parameter.NextEntryKey.Trim();
            }

            if (parameter.NextQueryEntryKey == null)
            {
                parameter.NextQueryEntryKey = "";
            } else
            {
                parameter.NextQueryEntryKey = parameter.NextQueryEntryKey.Trim();
            }

            if (parameter.ActionName == null)
            {
                parameter.ActionName = "";
            } else
            {
                parameter.ActionName = parameter.ActionName.Trim();
            }

            return parameter;
        }

        /// <summary>
        /// 检查客户端传递的参数
        /// </summary>
        /// <returns></returns>
        public static Result checkParameter(Parameter parameter)
        {
            List<string> errorMsg = new List<string>();
            Result rsA = checkModel(parameter);
            if (!rsA.success)
            {
                errorMsg.Add(rsA.msg);
            }

            if (parameter.ActionName == "Push")
            {
                Result rsB = checkLocalFormId(parameter);
                if (!rsB.success)
                {
                    errorMsg.Add(rsB.msg);
                }

                Result rsC = checkNextFormId(parameter);
                if (!rsC.success)
                {
                    errorMsg.Add(rsC.msg);
                }

                Result rsD = checkRuleId(parameter);
                if (!rsD.success)
                {
                    errorMsg.Add(rsD.msg);
                }

                Result rsE = checkNextEntryKey(parameter);
                if (!rsE.success)
                {
                    errorMsg.Add(rsE.msg);
                }
            }
            else if (parameter.ActionName == "Save")
            {
                Result rsC = checkLocalFormId(parameter);
                if (!rsC.success)
                {
                    errorMsg.Add(rsC.msg);
                }

                Result rsE = checkNextEntryKey(parameter);
                if (!rsE.success)
                {
                    errorMsg.Add(rsE.msg);
                }
            }
            else if (parameter.ActionName == "Delete")
            {
                Result rsC = checkLocalFormId(parameter);
                if (!rsC.success)
                {
                    errorMsg.Add(rsC.msg);
                }

                Result rsD = checkFIDAndBillNo(parameter);
                if (!rsD.success)
                {
                    errorMsg.Add(rsD.msg);
                }
            }
            else if (parameter.ActionName == "Audit")
            {
                Result rsC = checkLocalFormId(parameter);
                if (!rsC.success)
                {
                    errorMsg.Add(rsC.msg);
                }
            }
            else if (parameter.ActionName == "UnAudit")
            {
                Result rsC = checkLocalFormId(parameter);
                if (!rsC.success)
                {
                    errorMsg.Add(rsC.msg);
                }

                Result rsD = checkFIDAndBillNo(parameter);
                if (!rsD.success)
                {
                    errorMsg.Add(rsD.msg);
                }
            }
            else if (parameter.ActionName == "PushAndUpdate")
            {
                Result rsB = checkLocalFormId(parameter);
                if (!rsB.success)
                {
                    errorMsg.Add(rsB.msg);
                }

                Result rsC = checkNextFormId(parameter);
                if (!rsC.success)
                {
                    errorMsg.Add(rsC.msg);
                }

                Result rsD = checkRuleId(parameter);
                if (!rsD.success)
                {
                    errorMsg.Add(rsD.msg);
                }

                Result rsE = checkNextEntryKey(parameter);
                if (!rsE.success)
                {
                    errorMsg.Add(rsE.msg);
                }
            }
            else
            {
                errorMsg.Add("ActionName 参数配置错误");
            }

            Result result = new Result();
            if (errorMsg.Count() > 0)
            {
                result.msg = string.Join("；", errorMsg);
                return result;
            }

            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查 LocalFormId
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        private static Result checkLocalFormId(Parameter parameter)
        {
            Result result = new Result();
            if (parameter.LocalFormId.Length == 0)
            {
                result.msg = "请求参数中固定参数 LocalFormId 不能为空";
                return result;
            }
            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查 NextFormId
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Result checkNextFormId(Parameter parameter)
        {
            Result result = new Result();
            if (parameter.NextFormId.Length == 0)
            {
                result.msg = "请求参数中固定参数 NextFormId 不能为空";
                return result;
            }
            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查 RuleId
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Result checkRuleId(Parameter parameter)
        {
            Result result = new Result();
            if (parameter.RuleId.Length == 0)
            {
                result.msg = "请求参数中固定参数 RuleId 不能为空";
                return result;
            }
            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查 NextQueryEntryKey
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Result checkNextQueryEntryKey(Parameter parameter)
        {
            Result result = new Result();
            if (parameter.NextQueryEntryKey.Length == 0)
            {
                result.msg = "请求参数中固定参数 NextQueryEntryKey 不能为空";
                return result;
            }
            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查 NextEntryKey
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Result checkNextEntryKey(Parameter parameter)
        {
            Result result = new Result();
            if (parameter.NextEntryKey.Length == 0)
            {
                result.msg = "请求参数中固定参数 NextEntryKey 不能为空";
                return result;
            }
            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查 Model
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Result checkModel(Parameter parameter)
        {
            Result result = new Result();
            if (parameter.Model == null)
            {
                result.msg = "请求参数中固定参数 Model 不能为空";
                return result;
            }
            result.success = true;
            return result;
        }

        /// <summary>
        /// 检查业务参数中的 FID 和 FBillNo
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static Result checkFIDAndBillNo(Parameter parameter)
        {
            bool hasFID = false;
            bool hasFBillNo = false;
            if (parameter.Model.Property("FID") != null || Convert.ToInt64(parameter.Model["FID"]) > 0)
            {
                hasFID = true;
            }

            if (parameter.Model.Property("FBillNo") != null || Convert.ToString(parameter.Model["FBillNo"]).Length > 0)
            {
                hasFBillNo = true;
            }

            Result result = new Result();
            if (!hasFID && !hasFBillNo)
            {
                result.msg = "单据内码（FID）或者单据编码（FBillNo）不能同时为0或者为空";
                return result;
            }

            result.success = true;
            return result;
        }
    }
}
